package top.dancun.eascourse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.Query;

import top.dancun.eascourse.dao.CourseDao;
import top.dancun.eascourse.entity.CourseEntity;
import top.dancun.eascourse.service.CourseService;


@Service("courseService")
public class CourseServiceImpl extends ServiceImpl<CourseDao, CourseEntity> implements CourseService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CourseEntity> page = this.page(
                new Query<CourseEntity>().getPage(params),
                Wrappers.lambdaQuery(CourseEntity.class)
                        .like(Objects.nonNull(params.get("courseName"))
                                    &&StringUtils.isNotBlank(params.get("courseName").toString())
                                ,CourseEntity::getCourseName,
                                params.get("courseName")==null?"":String.valueOf(params.get("courseName")))
        );

        return new PageUtils(page);
    }

    @Override
    public List<CourseEntity> query(Map<String, String> params) {
        QueryWrapper<CourseEntity> wrapper = Wrappers.query();
        params.forEach((key,value)->{
            wrapper.eq(Objects.nonNull(value),key,value);
        });
        return this.baseMapper.selectList(wrapper);
    }

}